package guda.demox.web.config;


import guda.demox.common.mvc.filter.SessionStoreFilter;
import guda.demox.web.interceptor.LoggerInterceptor;
import guda.demox.web.security.AppAuthFilter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import javax.servlet.Filter;
import java.util.Locale;

/**
 * Created by well on 17/6/9.
 */

@EnableWebMvc
@Configuration
@EnableAutoConfiguration
@MapperScan(basePackages = {"guda.demox.dal", "guda.demox.dal.outer"})
@ComponentScan(basePackages = {"guda.demox"})
@ServletComponentScan
public class AppWebConfig implements WebMvcConfigurer {

    @Autowired
    private LoggerInterceptor loggerInterceptor;

    @Autowired
    private CookieConfig cookieConfig;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loggerInterceptor).addPathPatterns("/**");
        registry.addInterceptor(localeChangeInterceptor());
    }

//    @Bean
//    public FilterRegistrationBean cookieFilterRegistration() {
//
//        FilterRegistrationBean registration = new FilterRegistrationBean();
////        registration.setFilter(characterEncodingFilter());
////        registration.addUrlPatterns("/*");
////        registration.setName("characterEncodingFilter");
////        registration.setOrder(0);
//
//        registration = new FilterRegistrationBean();
//        registration.setFilter(cookieFilter());
//        registration.addUrlPatterns("/*");
//        registration.setName("cookieFilter");
//        registration.setOrder(1);
//
//
//        registration = new FilterRegistrationBean();
//        registration.setFilter(authContextFilter());
//        registration.addUrlPatterns("/*");
//        registration.setName("authContextFilter");
//        registration.setOrder(2);
//        return registration;
//    }

//    @Bean
//    public HttpMessageConverter<String> responseBodyConverter() {
//        StringHttpMessageConverter converter = new StringHttpMessageConverter(
//                Charset.forName("UTF-8"));
//        return converter;
//    }

    @Bean
    public Filter cookieFilter() {
        return new SessionStoreFilter(cookieConfig.getDomain(), cookieConfig.getMaxAge(), cookieConfig.getEncryptSalt(),
                cookieConfig.getCheckKey(), cookieConfig.getSessionKey());
    }

    @Bean
    public Filter authContextFilter() {
        return new AppAuthFilter("/blog/noAuth");
    }

    @Bean
    public Filter characterEncodingFilter() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);
        return characterEncodingFilter;
    }

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
        return slr;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        lci.setParamName("lang");
        return lci;
    }


    @Bean(name = "messageSource")
    public ResourceBundleMessageSource getMessageResource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }


}
